<?php 

namespace app\zhonghang\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
require_once (APP_PATH . 'api/SmsApi.php');
class User extends Base {
    //空操作
    public function _initialize(){
    	parent::_initialize();
    }
    public function _empty(){
    	
        header("HTTP/1.0 404 Not Found");
        $this->display('Public:404');
    }
    /**
     * 个人资料页面
     */
    public function index(){
    	$session = session::get();
		$member = Db::name('Users')->where(array('userId'=>$session['USER_KEY_ID']))->find();
		$member_relation = Db::name('App_member_relation')->where(array('member_id'=>$session['USER_KEY_ID']))->find();
// 		$tuijian = Db::name('App_member_relation')->where(array('member_id'=>$member_relation['zhuceid']))->find();
// 		$jiedian = Db::name('App_member_relation')->where(array('member_id'=>$member_relation['jiedianid']))->find();
		$tuijian = Db::name('Users')->where(array('userId'=>$member_relation['zhuceid']))->find();
		$jiedian = Db::name('Users')->where(array('userId'=>$member_relation['jiedianid']))->find();
		$sex = $member_relation['sex']=1?'男':'女';
		$quyu = $member_relation['quyu']=1?'左区':'右区';
		$this->assign('member_relation',$member_relation);
		$this->assign('sex',$sex);
		$this->assign('quyu',$quyu);
		$this->assign('member',$member);
		$this->assign('tuijian',$tuijian);
		$this->assign('jiedian',$jiedian);
		return $this->fetch();
    }
    /**
     * 个人资料修改页面
     */
    public function revise_massage(){
    	$session = session::get();
    	$member = Db::name('Users')->where(array('userId'=>$session['USER_KEY_ID']))->find();
    	$member_relation = Db::name('App_member_relation')->where(array('member_id'=>$session['USER_KEY_ID']))->find();
    	$this->assign('member_relation',$member_relation);
    	$this->assign('member',$member);
    	return $this->fetch();
    }
    public function revise_password(){
    	
    	return $this->fetch();
    }
     
    //会员结构  系谱图
    public function xiputu(){

    	//二级密码验证
    	//$this->checkTwoPwd();
    	session('checkTwoPwd_success',null);
    	//程序
    	$username=I('get.username');
    	$username=!empty($username)?$username:$this->member['nick'];//获取前台传递的username，默认的就是本人
		$user=!empty($username)?M('Member')->where("nick='$username'")->find():$this->member;
		
		//dump($user);echo M('Member')->_sql();die;
		$this->assign('user',$user);
    	$this->assign('username',$username);
    	$one=M('Member')->where("nick='$username'")->find();
    	$two_one=$this->getUperUsername($one['member_id'],left);//A区
    	$two_two=$this->getUperUsername($one['member_id'],cent);//B区
    	$two_three=$this->getUperUsername($one['member_id'],right);//C区
    	$three_one=$this->getUperUsername($two_one['member_id'],left);//A区
    	$three_two=$this->getUperUsername($two_one['member_id'],cent);//B区
    	$three_three=$this->getUperUsername($two_one['member_id'],right);//C区
    	$three_four=$this->getUperUsername($two_two['member_id'],left);//A区
    	$three_five=$this->getUperUsername($two_two['member_id'],cent);//B区
    	$three_six=$this->getUperUsername($two_two['member_id'],right);//C区
    	$three_seven=$this->getUperUsername($two_three['member_id'],left);//A区
    	$three_eight=$this->getUperUsername($two_three['member_id'],cent);//B区
    	$three_nine=$this->getUperUsername($two_three['member_id'],right);//C区
    	$this->assign('one',$one);
    	$this->assign('two_one',$two_one);
    	$this->assign("two_two",$two_two);
    	$this->assign("two_three",$two_three);
    	$this->assign("three_one",$three_one);
    	$this->assign("three_two",$three_two);
    	$this->assign("three_three",$three_three);
    	$this->assign("three_four",$three_four);
    	$this->assign("three_five",$three_five);
    	$this->assign("three_six",$three_six);
    	$this->assign("three_seven",$three_seven);
    	$this->assign("three_eight",$three_eight);
    	$this->assign("three_nine",$three_nine);
    	$this->assign('level',$_POST['level']);
    	return $this->fetch();
    }
    //获取当前人的下线
    private function getUperUsername($id,$quyu){
    	$where['pid']=$id;
    	$where['quyu']=$quyu;
    	$list=M('Member')->where($where)->order("quyu asc")->find();
    	return $list;
    }
	
    
    
    public function updatePwdOne(){
    	//手机号
    	$phone = input('phone');
    	//验证码
    	$code = input('code');
    	//验证验证码
    	$A_Sms= new \SmsApi();
    	$verifyPhone = $A_Sms ->checkAppCode($phone, $code);
    	if($verifyPhone){
    		$sendtime = json_decode($A_Sms ->getSms($phone));
    		$name = 'time_quyum_' . $phone;
    		$time = $sendtime ->$name;
    		$ctime = time();
    		$chatime = $ctime-$time;
    		if($chatime>600){
    			//删除验证码
    			$A_Sms ->deleteSendSmsApp($phone);
    			$data['status']= -10;
    			$data['info']= '验证码失效(有效时间10分钟)';
    			return json($data);
    		}
    	}
    	 
    	if(!$verifyPhone){
    		$data['status']= -5;
    		$data['info']= '手机验证码输入有误';
    		return json($data);
    	}
    	
    	$data['status'] = 1;
    	$data['info'] = '提交成功';
    	return json($data);
    	
    }
    
    /**
     * 修改登录密码
     */
    public function updatePwd(){
    	$member = $this->member;
    	$new_password     = input('newpwd_one');
    	$confirm_password = input('newpwd_two');

    	
    	
    	$member_id = $member['member_id'];
    	$user = Db::name('Users')->where(array('userId'=>$member_id))->find();
  		if(md5($new_password)!=md5($confirm_password)){
    		$data['status'] = -2;
    		$data['info'] = '您的确认密码输入有误';
    		return json($data);
    	}
    	if(md5($new_password) == $user['loginPwd']){
    		$data['status'] = -3;
    		$data['info'] = '新密码不能与旧密码一致';
    		return json($data);
    	}
//     	if(!checkPwd($new_password)){
//     		$data['status'] = -4;
//     		$data['info'] = '请输入正确的密码格式';
//     		return json($data);
//     	}
//     	if(md5($new_password) == $user['payPwd']){
//     		$data['status'] = -5;
//     		$data['info'] = '新密码不能与安全密码一致';
//     		return json($data);
//     	}
    	//修改数据库
    	$data['loginPwd']        = md5($new_password);
    	$r = Db::name('Users')->where(array('userId'=>$member_id))->update($data);
    	if($r){
    		$data['status'] = 1;
    		$data['info'] = '提交成功';
    		return json($data);
    	}else{
    		$data['status'] = -6;
    		$data['info'] = '服务器繁忙,请稍后重试';
    		return json($data);
    	}
    }
    /**
     * 修改安全密码
     */
    public function updateTradePwd(){
    	$member = $this->member;
    	//手机号
    	$phone = input('phone');
    	//验证码
    	$code = input('code');
    	$new_password     = input('newpwd_onet');
    	$confirm_password = input('newpwd_twot');
    	$member_id = $member['member_id'];
    	$user = Db::name('Users')->where(array('userId'=>$member_id))->find();
    	//验证验证码
    	$A_Sms= new \SmsApi();
    	$verifyPhone = $A_Sms ->checkAppCode($phone, $code);
    	if($verifyPhone){
    		$sendtime = json_decode($A_Sms ->getSms($phone));
    		$name = 'time_quyum_' . $phone;
    		$time = $sendtime ->$name;
    		$ctime = time();
    		$chatime = $ctime-$time;
    		if($chatime>600){
    			//删除验证码
    			$A_Sms ->deleteSendSmsApp($phone);
    			$data['status']= -10;
    			$data['info']= '验证码失效(有效时间10分钟)';
    			return json($data);
    		}
    	}
    	 
    	if(!$verifyPhone){
    		$data['status']= -5;
    		$data['info']= '手机验证码输入有误';
    		return json($data);
    	}
    	if(md5($new_password)!=md5($confirm_password)){
    		$data['status'] = -2;
    		$data['info'] = '您的确认密码输入有误';
    		return json($data);
    	}
    	if(md5($new_password) == $user['payPwd']){
    		$data['status'] = -3;
    		$data['info'] = '新密码不能与旧密码一致';
    		return json($data);
    	}
//     	if(!checkPwd($new_password)){
//     		$data['status'] = -4;
//     		$data['info'] = '请输入正确的密码格式';
//     		return json($data);
//     	}
    	if(md5($new_password) == $user['loginPwd']){
    		$data['status'] = -5;
    		$data['info'] = '新密码不能与登录密码一致';
    		return json($data);
    	}
    	//修改数据库
    	$data['payPwd']  = md5($new_password);
    	$r = Db::name('Users')->where(array('userId'=>$member_id))->save($data);
    	if($r){
    		$data['status'] = 1;
    		$data['info'] = '提交成功';
    		return json($data);
    	}else{
    		$data['status'] = -6;
    		$data['info'] = '服务器繁忙,请稍后重试';
    		return json($data);
    	}
    }
    /**
     * 修改手机号码
     * 
     */
    public function updatePhone(){
    	$member = $this->member;
    	$phone_old = input('phone_old');
    	$code_old = input('code_old');
    	$phone_new = input('phone_new');
    	$code_new = input('code_new');
    	if($phone_old != $member['phone']){
    		$data['status'] = -1;
    		$data['info'] = '旧手机与预留手机不符';
    		return json($data);	
    	}
    	if(empty($code_old) || empty($code_new)){
    		$data['status'] = -2;
    		$data['info'] = '补全验证码';
    		return json($data);
    	}
    	//验证验证码
    	$A_Sms= new \SmsApi();
    	$verifyPhone_old = $A_Sms ->checkAppCode($phone_old, $code_old);
    	if($verifyPhone_old){
    		$sendtime_old = json_decode($A_Sms ->getSms($phone_old));
    		$name_old = 'time_quyum_' . $phone_old;
    		$time_old = $sendtime_old ->$name_old;
    		$ctime_old = time();
    		$chatime_old = $ctime_old-$time_old;
    		if($chatime_old>600){
    			//删除验证码
    			$A_Sms ->deleteSendSmsApp($phone_old);
    			$data['status']= -10;
    			$data['info']= '验证码失效(有效时间10分钟)';
    			return json($data);
    		}
    	}
    	if(!$verifyPhone_old){
    		$data['status']= -5;
    		$data['info']= '手机验证码输入有误';
    		return json($data);
    	}
    	
    	$verifyPhone_new = $A_Sms ->checkAppCode($phone_new, $code_new);
    	if($verifyPhone_new){
    		$sendtime_new = json_decode($A_Sms ->getSms($phone_new));
    		$name_new = 'time_quyum_' . $phone_new;
    		$time_new = $sendtime_new ->$name_new;
    		$ctime_new = time();
    		$chatime_new = $ctime_new-$time_new;
    		if($chatime_new>600){
    			//删除验证码
    			$A_Sms ->deleteSendSmsApp($phone_new);
    			$data['status']= -10;
    			$data['info']= '验证码失效(有效时间10分钟)';
    			return json($data);
    		}
    	}
    	if(!$verifyPhone_new){
    		$data['status']= -5;
    		$data['info']= '手机验证码输入有误';
    		return json($data);
    	}
    	$list['phone'] = $phone_new;
    	$res = Db::name('App_member_relation')->where(array('member_id'=>$member['member_id']))->update($list);
    	if($res){
    		$data['status']= 1;
    		$data['info']= '修改成功';
    		return json($data);
    	}else{
    		$data['status']= -6;
    		$data['info']= '失败';
    		return json($data);
    	}
    }
    
    
    
    
    
   
    // 推介图
    public function tuijian(){
// 		$uname=isset($_POST['uname'])?$_POST['uname']:$_SESSION['username'];
		$uname=$_SESSION['USER_KEY'];
		$user=M('Member')->where("email='".$this->member['email']."'")->find();
		$my=$this->getMemberByMemberId($_SESSION['USER_KEY_ID']);
// 		if (!$user) {
// 			$this->error("无此会员信息");
// 		}
// 		$user['utype']=$this->getTypeNameById($user['utype']);
		$down_user=M('Member')->where("zhuceid='{$user['member_id']}'")->select();
		$this->assign('down',$down_user);
		$this->assign('user',$user);
		$this->assign('my',$my);
		$this->display ();
    }

    public function getInivttuijian() {
    	$username = I('username');
    	$user=$this->getMemberByUsername($username);
    	$U = M ( 'Member' );
    	$r = $U->where ( "zhuceid=".$user['member_id'] )->select ();
    	if (! empty ( $r )) {
    		$data ['status'] = 1;
    		$data ['list'] = $r;
    		return json ( $data );
    	} else {
    		$data ['status'] = 0;
    		return json ( $data );
    	}
    }
    /**
     * 获得用户银行信息
     */
    public function getMemberBankInfor(){
    	$member = $this->member;
    	$list['kaihuhang']      =   $member['kaihuhang'];
    	$list['bank_card']      =    $member['bank_card'];
    	$list['kaihuren']       =    $member['kaihuren'];
    	$list['bank_address']       =    $member['bank_address'];
    	$list['zhifubao']       =   $member['zhifubao'];
    	
    	$data['info'] ='成功';
    	$data['status'] = 1;
    	$data['list'] = $list;
    	return json($data);
    }
    
    
    
    /**
     * 会员信息修改
     */
    public function save_member(){
    	
    	$member = $this->member;
    	if(!empty($member['kaihuhang']) || !empty($member['bank_card']) || !empty($member['kaihuren']) || !empty($member['bank_address']) || !empty($member['zhifubao'])){
    		$arr['status'] = -5;
    		$arr['info'] = '无法修改请联系管理员';
    		return json($arr);
    	}
    	$data['kaihuhang']      =    input('kaihuhang')?input('kaihuhang'):null;
    	$data['bank_card']      =    input('bank_card')?input('bank_card'):null;
    	$data['kaihuren']       =    input('kaihuren')?input('kaihuren'):null;
    	$data['bank_address']       =    input('bank_address')?input('bank_address'):null;
    	$data['zhifubao']       =    input('zhifubao')?input('zhifubao'):null;
    	
    	if($data['bank_card']){
    		$res = $this->regex('bankcard', $data['bank_card']);
    		if(!$res){
    			$arr['status'] = -1;
    			$arr['info'] = '请输入正确的银行卡号';
    			return json($arr);
    		}
    	}
    	foreach($data as $v){
    		if(empty($v)){
    			$arr['status']=-1;
    			$arr['info']="请补全信息";
    			return json($arr);exit();
    		}
    	}
    	$re = DB::name("App_member_relation")->where(array('member_id'=>$member['member_id']))->update($data);
    	if($re){
    		$arr['status']= 1;
    		$arr['info']="修改成功";
    	}else{
    		$arr['status']=-4;
    		$arr['info']="修改失败";
    	}
    	return json($arr);
    }
    
    
    
    
    /**
     * 正则验证
     * @param unknown $rule
     * @param unknown $value
     * @return boolean
     */
    function regex($rule,$value) {
    	$validate = array(
    			'email'     =>  '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
    			'url'       =>  '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(:\d+)?(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/',
    			'currency'  =>  '/^\d+(\.\d+)?$/',
    			'number'    =>  '/^\d+$/',
    			'num'    	=>  '/^[0-9]*$/',
    			'zip'       =>  '/^\d{6}$/',
    			'integer'   =>  '/^[-\+]?\d+$/',
    			'double'    =>  '/^[-\+]?\d+(\.\d+)?$/',
    			'english'   =>  '/^[A-Za-z]+$/',
    			'idcard'	=>	'/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/i' ,
    			// 			'name'		=>  '/[^\D]/g',
    			'img'		=>	'/\.(jpg|gif|png)$/i',
    			'phone'		=>  '#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#',
    			'password'  =>  '/^[\w-\.]{6,16}$/',
    			'bankcard'  =>  '/^(\d{16}|\d{19})$/',
    			'username'  =>  '/^[a-zA-Z][a-zA-Z0-9_]{1,7}$/',
    	);
    	// 检查是否有内置的正则表达式
    	if(isset($validate[strtolower($rule)])){
    		$rule       =   $validate[strtolower($rule)];
    	}
    	return preg_match($rule,$value);
    }
    
    /**
     * 我得订单
     * business_type 1买     2卖
     * order_status : 5挂单中  0待支付  1待确认 2已完成 3已取消 4申诉中
     */
    public function getOrderAndTrade(){
    	$business_type = input('business_type');
    	$order_status = input('order_status');
    	$member = $this->member;
    	
    	if($order_status == 5){
    		//挂单中的订单
    		$db = 'Orders';
    		$where['status'] = 0;
    		$where['member_id'] = $member['member_id'];
    		if($business_type == 1){
    			$where['type'] = 'buy';
    		}else{
    			$where['type'] = 'sell';
    		}
    		
    	}else{
    		//开始交易的订单
    		$db = 'Trade';
    		$where['status'] = $order_status;
    		if($business_type == 1){
    			$where['buy_member_id'] = $member['member_id'];
    		}else{
    			$where['sell_member_id'] = $member['member_id'];
    		}
    	}
//     	dump($db);
//     	dump($where);
    	$list = Db::name($db)->where($where)->select();
//     	dump($list);
    	if($list){
    		$data['data'] = $list;
    		$data['info'] = '成功';
    		$data['status'] = 1;
    		return json($data);
    	}else{
    		$data['data'] = array();
    		$data['info'] = '暂无记录';
    		$data['status'] = -1;
    		return json($data);
    	}
    }
    
    
    /**
     * 获得对方信息
     */
    public function getOtherSide(){
    	$member = $this->member;
    	$trade_id = input('id');
    	$trade = Db::name('Trade')->where(array('id'=>$trade_id))->find();
    	if($member['member_id'] == $trade['sell_member_id']){
    		$otherSide_member_id = $trade['buy_member_id'];
    	}else{
    		$otherSide_member_id = $trade['sell_member_id'];
    	}
    	
    	$otherSide_member = Db::name('App_member_relation')->where(array('member_id'=>$otherSide_member_id))->find();
    	$list['phone'] = $otherSide_member['phone'];
    	$list['zhifubao'] = $otherSide_member['zhifubao'];
    	$list['kaihuren'] = $otherSide_member['kaihuren'];
    	$list['bank_card'] = $otherSide_member['bank_card'];
    	$list['kaihuhang'] = $otherSide_member['kaihuhang'];
    	$list['bank_address'] = $otherSide_member['bank_address'];
    	if($otherSide_member){
    		$data['info'] = '查询成功';
    		$data['status'] = 1;
    		$data['list'] = $list;
    		return json($data);
    	}else{
    		$data['info'] = '失败';
    		$data['status'] = -1;
    		$data['list'] = $list;
    		return json($data);
    	}
    }
    /**
     * 上传凭证
     */
    public function upPic(){
    	$member = $this->member;
    	$content = input('content');
    	$trade_id = input('id');
    	$trade = Db::name('Trade')->where(array('id'=>$trade_id))->find();
    	if($trade['sell_member_id'] == $member['member_id']){
    		$data['info'] = '卖家无法上传';
    		$data['status'] = -1;
    		return json($data);
    	}
    	$file = request()->file('pic');
    	if(empty($file)){
    		$data['info'] = '不能为空';
    		$data['status'] = -2;
    		return json($data);
    	}
    	$info = $file->move ( ROOT_PATH . 'public' . DS . 'uploads' );
    	$header_img = '/uploads/' . str_replace('\\', '/', $info->getsaveName ());
    	$arr['trade_id'] = $trade_id; 
    	$arr['pic'] = $header_img;
    	$arr['content'] = $content;
    	$arr['add_time'] = time();
    	$res = Db::name('Trade_pic')->insert($arr);
    	if($res){
    		$data['info'] = '成功';
    		$data['status'] = 1;
    		return json($data);
    	}else{
    		$data['info'] = '失败';
    		$data['status'] = -3;
    		return json($data);
    	}
    }
    /**
     * 查看凭证
     * @return Ambigous <\think\response\Json, \think\Response, \think\response\View, \think\response\Xml, \think\response\Redirect, \think\response\Jsonp, unknown, \think\Response>
     */
    public function getPic(){
    	$member = $this->member;
    	$trade_id = input('id');
    	$list = Db::name('Trade_pic')->where(array('trade_id'=>$trade_id))->select();
    	
    	if(empty($list)){
    		$data['list'] = array();
    		$data['info'] = '暂无信息';
    		$data['status'] = -1;
    		return json($data);
    	}else{
    		$data['list'] = $list;
    		$data['info'] = '成功';
    		$data['status'] = 1;
    		return json($data);
    	}
    }
    /**
     * 确认付款
     * 
     */
    public function confirmGive(){
    	$member = $this->member;
    	$trade_id = input('id');
    	$payPwd = input('payPwd');
    	$user = Db::name('Users')->where(array('userId'=>$member['member_id']))->find();
    	if($user['payPwd'] != md5($payPwd)){
    		$data['info'] = '二级密码错误';
    		$data['status'] = -7;
    		return json($data);
    	}
    	$trade = Db::name('Trade')->where(array('id'=>$trade_id))->find();
    	//判断是不是当前订单的买家
    	if($trade['buy_member_id'] != $member['member_id']){
    		$data['info'] = '暂无该订单权限';
    		$data['status'] = -1;
    		return json($data);
    	}
    	//判断订单状态
    	if($trade['status'] != 0){
    		$data['info'] = '当前订单无法操作';
    		$data['status'] = -2;
    		return json($data);
    	}
    	//判断是否已经确认付款
    	if($trade['whether_give'] == 1){
    		$data['info'] = '请勿重复确认';
    		$data['status'] = -3;
    		return json($data);
    	}
//     	//判断是否上传凭证
//     	if(empty($trade['pic'])){
//     		$data['info'] = '请先上传凭证';
//     		$data['status'] = -4;
//     		return json($data);
//     	}
    	//判断是否超出规定打款时间TODO
    	
    	$arr['whether_give'] = 1;
    	$arr['give_time'] = time();
    	$arr['status'] = 1;
    	$res =  Db::name('Trade')->where(array('id'=>$trade_id))->update($arr);
    	if($res){
    		$data['info'] = '确认成功';
    		$data['status'] = 1;
    		return json($data);
    	}else{
    		$data['info'] = '确认失败';
    		$data['status'] = -5;
    		return json($data);
    	}
    }
    /**
     * 确认收款
     */
    public function confirmGet(){
    	$member = $this->member;
    	$trade_id = input('id');
    	$payPwd = input('payPwd');
    	$user = Db::name('Users')->where(array('userId'=>$member['member_id']))->find();
    	if($user['payPwd'] != md5($payPwd)){
    		$data['info'] = '二级密码错误';
    		$data['status'] = -7;
    		return json($data);
    	}
    	$trade = Db::name('Trade')->where(array('id'=>$trade_id))->find();
    	//判断是不是当前订单的买家
    	if($trade['sell_member_id'] != $member['member_id']){
    		$data['info'] = '暂无该订单权限';
    		$data['status'] = -1;
    		return json($data);
    	}
    	//判断是否已经确认付款
    	if($trade['whether_get'] == 1){
    		$data['info'] = '请勿重复确认';
    		$data['status'] = -2;
    		return json($data);
    	}
//     	//判断是否上传凭证
//     	if(empty($trade['pic'])){
//     		$data['info'] = '未上传凭证，请等待';
//     		$data['status'] = -3;
//     		return json($data);
//     	}
    	//判断是否确认付款
    	if($trade['whether_give'] != 1){
    		$data['info'] = '未确认付款，请等待';
    		$data['status'] = -4;
    		return json($data);
    	}
    	//判断订单状态
    	if($trade['status'] != 1){
    		$data['info'] = '当前订单无法操作';
    		$data['status'] = -5;
    		return json($data);
    	}
    	$arr['whether_get'] = 1;
    	$arr['get_time'] = time();
    	$arr['com_time'] = time();
    	$arr['status'] = 2;
    	$res[] =  Db::name('Trade')->where(array('id'=>$trade_id))->update($arr);
    	//然后给买家加钱
    	$num = $trade['num'];
    	$res[] = Db::name('App_member_account')->where(array('member_id'=>$trade['buy_member_id']))->setInc('account_type_3',$num);
    	
    	if($res){
    		$data['info'] = '确认成功';
    		$data['status'] = 1;
    		return json($data);
    	}else{
    		$data['info'] = '确认失败';
    		$data['status'] = -5;
    		return json($data);
    	}
    }
    
    /**
     * 上传凭证
     */
    public function upPicShenShu(){
    	$member = $this->member;
    	$content = input('content');
    	$trade_id = input('id');
    	
    	//$trade = Db::name('Trade')->where(array('id'=>$trade_id))->find();
    	
    	$file = request()->file('pic');
    	if(empty($file)){
    		$data['info'] = '不能为空';
    		$data['status'] = -2;
    		return json($data);
    	}
    	$info = $file->move ( ROOT_PATH . 'public' . DS . 'uploads' );
    	$header_img = '/uploads/' . str_replace('\\', '/', $info->getsaveName ());
    	$arr_shen['trade_id'] = $trade_id;
    	$arr_shen['pic'] = $header_img;
    	$arr_shen['content'] = $content;
    	$arr_shen['add_time'] = time();
    	$res = Db::name('Shenshu_pic')->insert($arr_shen);
    	$arr['status'] = 4;
    	$res = Db::name('Trade')->where(array('id'=>$trade_id))->update($arr);
    	
    	if($res){
    		$data['info'] = '成功';
    		$data['status'] = 1;
    		return json($data);
    	}else{
    		$data['info'] = '失败';
    		$data['status'] = -3;
    		return json($data);
    	}
    }
    
    /**
     * 申诉
     */
    public function shenshu(){
    	$member = $this->member;
    	$trade_id = input('id');
    	
    	$arr['status'] = 4;
    	$res = Db::name('Trade')->where(array('id'=>$trade_id))->update($arr);
   		if($res){
   			$data['info'] = '成功';
   			$data['status'] = 1;
   			return json($data);
   		}else{
   			$data['info'] = '失败';
   			$data['status'] = -1;
   			return json($data);
   		}
    }
    
    
    /**
     * 挂单取消
     */
    public function quxiao(){
    	$order_id = input('id');
    	$order = Db::name('Orders')->where(array('id'=>$order_id))->find();
    	if($order['type'] == 'sell'){
    		$num = $order['num'] + $order['fee'];
    		$res = Db::name('App_member_account')->where(array('member_id'=>$order['member_id']))->setInc('account_type_3',$num);
    	}
    	$arr['status'] = 2;
    	$res = Db::name('Orders')->where(array('id'=>$order_id))->update($arr);
    	if($res){
    		$data['info'] = '成功';
    		$data['status'] = 1;
    		return json($data);
    	}else{
    		$data['info'] = '失败';
    		$data['status'] = -1;
    		return json($data);
    	}
    }
    
    
    
    
    
    
    /**
     * 我得团队
     * @return Ambigous <\think\response\Json, \think\Response, \think\response\View, \think\response\Xml, \think\response\Redirect, \think\response\Jsonp, unknown, \think\Response>
     */
    public function myTeam(){
    	$loginName = input('loginName');
    	$user = Db::name('Users')->where(array('loginName'=>$loginName))->find();
    	$where['zhuceid'] = $user['userId'];
    	$list = Db::name('App_member_relation')->where($where)->select();
    	foreach($list as $k=>$v){
    		$user = Db::name('Users')->where(array('userId'=>$list[$k]['member_id']))->find();
    		$list[$k]['loginName'] = $user['loginName'];
    	}
    	if($list){
    		$data['data'] = $list;
    		$data['info'] = '成功';
    		$data['status'] = 1;
    		return json($data);	
    	}else{
    		$data['data'] = array();
    		$data['info'] = '暂无记录';
    		$data['status'] = -1;
    		return json($data);
    	}	
    }
    /**
     * 个人中心公告
     * @return Ambigous <\think\response\Json, \think\Response, \think\response\View, \think\response\Xml, \think\response\Redirect, \think\response\Jsonp, unknown, \think\Response>
     */
    public function artList(){
    	$list = Db::name('App_article')->field('article_id,title,content,add_time')->select();
    	foreach ($list as $k=>$v){
    		$list[$k]['title']=mb_substr((strip_tags(html_entity_decode($v['title']))),0,14,'utf-8');
    		$list[$k]['content']=mb_substr((strip_tags(html_entity_decode($v['content']))),0,30,'utf-8');
    	}
    	if($list){
    		$data['art'] = $list;
    		$data['info'] = '成功';
    		$data['status'] = 1;
    		return json($data);	
    	}else{
    		$data['art'] = array();
    		$data['info'] = '暂无记录';
    		$data['status'] = -1;
    		return json($data);
    	}
    }
    /**
     * 公告详情
     */
    public function artDetail(){
    	$article_id = input('article_id');
    	$list = Db::name('App_article')->field('title,content,add_time')->where(array('article_id'=>$article_id))->find();
    	if($list){
    		$data['art'] = $list;
    		$data['info'] = '成功';
    		$data['status'] = 1;
    		return json($data);
    	}else{
    		$data['art'] = $list;
    		$data['info'] = '暂无记录';
    		$data['status'] = -1;
    		return json($data);
    	}
    }
    /**
     * 获得意见反馈
     */
    public function getYiJianList(){
    	$member = $this->member;
    	$where['member_id'] = $member['member_id'];
    	$list = Db::name('App_yijian')->where($where)->select();
    	if($list){
    		$data['art'] = $list;
    		$data['info'] = '成功';
    		$data['status'] = 1;
    		return json($data);
    	}else{
    		$data['art'] = $list;
    		$data['info'] = '暂无记录';
    		$data['status'] = -1;
    		return json($data);
    	}
    }
    
    
    
    /**
     * 意见反馈
     * @return Ambigous <\think\response\Json, \think\Response, \think\response\View, \think\response\Xml, \think\response\Redirect, \think\response\Jsonp, unknown, \think\Response>
     */
    public function yijianFan(){
    	$member = $this->member;
    	$content = input('content');
    	$arr['member_id'] = $member['member_id'];
    	$arr['content'] = $content;
    	$arr['status'] = 0;
    	$arr['add_time'] = time();
    	$res = Db::name('App_yijian')->insert($arr);
    	if($res){
    		$data['info'] = '成功';
    		$data['status'] = 1;
    		return json($data);
    	}else{
    		$data['info'] = '失败';
    		$data['status'] = -1;
    		return json($data);
    	}
    	
    }
    
    
    
    
    
    
    
    
    
    
    public  function updateTx(){
    	$id = $_SESSION['USER_KEY_ID'];
    	$upload = new \Think\Upload();// 实例化上传类
    	$upload->maxSize   =     3145728 ;// 设置附件上传大小
    	$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
    	$upload->savePath  =      './goods/'; // 设置附件上传目录
    	// 上传文件
    	$info   =   $upload->upload();
    	if(!$info) {// 上传错误提示错误信息
    		if(!empty($id)){
    			$c_pic=$_POST['c_pic'];
    		}else{
    			$this->error($upload->getError());
    		}
    	}else{// 上传成功
    		$pic=$info['file']['savepath'].$info['file']['savename'];
    		$c_pic='/Uploads'.ltrim($pic,".");
    	}
    	 
    	$_POST['pic']=$c_pic;
    	$re=M("Member")->where(['member_id'=>$id])->save($_POST);
    	
    	//dump(M('Goods')->_sql());
    	//die();
    	if($re){
    		$data['status']=1;
    		$data['info']="修改成功,请稍等....";
    		return json($data);
    	}else{
    		$data['status']=-1;
    		$data['info']="修改失败,请稍等....";
    		return json($data);
    	}
    }


    /**
     * 获取我的订单
     */
    public function getMyOrders(){
        $member = $this->member;
//         $where['status'] = 2;   //2表示已经收款
        $where['member_id'] = $member['member_id'];
        $order = Db::name('Orders')->where($where)->order('add_time desc')->select();
        
        if($order){
            foreach($order as $k=>$v){
                $goods_info = Db::name('goods')->where('id',$v['gid'])->find();   //获取商品信息
                $order[$k]['goods'] = $goods_info;
            }
            $data['data'] = $order;
            $data['info'] = '成功';
            $data['status'] = 1;
            return json($data);
        }else{
            $data['data'] = array();
            $data['info'] = '暂无记录';
            $data['status'] = -1;
            return json($data);
        }
    }

    //返回订单物流信息
    public function getWuliuInfo(){
        $member = $this->member;
        $order_id = input('order_id');   //订单号
        $order = Db::name('orders')->where('id',$order_id)->find();  //订单信息
        if($order){
            $arr['info'] = '查询成功';
            $arr['status'] = 1;
            $arr['data']['num'] = $order['kuaidi'];
        }else{
            $re['info'] = '暂无信息';
            $re['status'] = -1;
            $arr['data']= $re;
        }
        return json($arr);
    }
    
    
    
    
    
    
}